<?php
/**
 * 用户进去程序逻辑处理
 * User：LLW
 * Date：2020-03-23
 */

namespace app\api\model;

use think\Db;
use think\Model;
use think\Session;

class LoginModel extends Model
{
    /**
     * 用户登录
     * @throws \think\Exception
     */
    public function postLogin()
    {
        $data = input('post.');
        if(count($data)<=1){
            AjaxReturn(-1,'缺少数据');

        }
        $find = \db('user')->where('us_phone',$data['us_name'])->find();
        $data['us_pwd'] = md5($data['us_pwd']);
        if(empty($find)){
            AjaxReturn(-1,'用户不存在');
        }
        if($find['us_pwd'] != $data['us_pwd']){
            AjaxReturn(-1,'密码不正确');
        }
        if($find['us_state'] == 2){
            AjaxReturn(-1,'您的账户已被锁定');
        }
        //存取登录状态
        $data['us_ltime'] = time();
        $data['us_ip'] = request()->ip();
        $res = Db::name('user')->where('us_uid',$find['us_uid'])->update($data);
        if($res){
            accountLog(1,2,$find['us_phone'],'会员登录前台');//记录会员操作信息
            Session::set('uid',$find['us_uid']);//存入Session
            AjaxReturn(1,'登录成功');
        }else{
            AjaxReturn(-1,'网络获取失败，请退出重试');
        }
    }

    /**
     * 会员注册逻辑
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function postRegister()
    {
        //初始化相关数据
        $usident = Db::name('user');
        $data = input('post.');

        //查询用户输入信息
        $find = $usident->where('us_phone',$data['us_phone'])->find();

        $data['us_pwd'] = md5($data['us_pwd']);
        $str = getRandomStr();
        $find1 = $usident->where('us_pwds',$str)->find();
        if($find1){
            AjaxReturn(-1,'财富密钥重复，请重新点击提交！');
        }

        $data['us_pwds'] = $str;
        if(!empty($data['us_pid'])){
            $pid = $usident->where('us_uid',$data['us_pid'])->find();
            if(empty($pid)){
                AjaxReturn(-1,'推荐人信息不存在');
            }
            $data['us_pid'] = $pid['us_uid'];
        }
        if(!empty($find)){
            AjaxReturn(-1,'该手机号已经被注册,您可申请找回密码');
        }

        //判断验证码得有效性
        $code = Db::name('code_time')->where(['ct_code'=>$data['code'],'ct_phone'=>$data['us_phone']])->find();
        if(empty($code)){
            AjaxReturn(-1,'验证码不正确');
        }
        if(time() > $code['ct_ltime']){
            AjaxReturn(-1,'验证码已过期');
        }

        //开始注册
        unset($data['code']);
        $data['us_uid'] = $data['us_phone'];
        $data['us_head'] = '/uploads/config/head.jpg';
        $data['us_atime'] = time();
        $data['us_name'] = time();
        $data['us_img_code'] = verificationCode($data['us_uid']);
        $res = $usident->insert($data);
        if($res){
            $date['str'] = $str;
            $date['msg'] = '注册成功!请保存财富密钥';
            AjaxReturn(1,$date);
        }else{
            AjaxReturn(-1,'注册失败');
        }
    }

    /**
     * 发送短信
     * @param $phone 发送用户
     * @param $type 1:注册 2:忘记 3:登录
     * return json
     */
    public function Sendmsg($phone = '',$type)
    {

        if(empty($phone)){
            AjaxReturn(-1,'手机号不可为空');
        }
        $code = rand_nums();
        $res = send_sms($phone,$code);
        if($res->Code == 'OK'){
            $data['ct_code'] = $code;
            $data['ct_phone'] = $phone;
            $data['ct_type'] = $type;
            $data['ct_atime'] = time();
            $data['ct_ltime'] = strtotime("+2 minutes");//失效时间

            $id = Db::name('code_time')->insertGetId($data);
            return array('code'=>1,'msg'=>'发送成功');
        }else{
            return array('code'=>-1,'msg'=>$res->Message,'data'=>'');
        }
    }

    /**
     * 忘记密码逻辑
     * @throws \think\Exception
     */
    public function postPwd()
    {
        $pwd = input('pwd','123456');//密码
        $code = input('code',null);//验证码
        $phone = input('us_phone',null);//发送者手机号
        $find = Db::name('user')->where('us_phone',$phone)->find();
        $code = Db::name('code_time')->where(['ct_phone'=>$phone,'ct_code'=>$code])->find();
        if(empty($find)){
            AjaxReturn(-1,'用户不存在');
        }
        if(empty($code)){
            AjaxReturn(-1,'验证码不正确');
        }
        if(time() > $code['ct_ltime']){
            AjaxReturn(-1,'验证码已过期');
        }
        if($find['us_pwd'] == md5($phone)){
            AjaxReturn(-1,'与原密码相同');
        }
        $res = Db::name('user')->where('us_phone',$phone)->update(['us_pwd'=>md5($pwd)]);
        if($res){
            accountLog(2,2,$phone,'找回密码');
            AjaxReturn(1,'修改成功');
        }else{
            AjaxReturn(-1,'网络错误！');
        }
    }
}